package com.ruoyi.web.controller.base;

import com.ruoyi.base.domain.BusWaterSegment;
import com.ruoyi.base.domain.SysApiapplyData;
import com.ruoyi.base.domain.SysPermission;
import com.ruoyi.base.service.IBusWaterSegmentService;
import com.ruoyi.base.service.IDataShareService;
import com.ruoyi.base.service.ISysApiapplyDataService;
import com.ruoyi.base.service.ISysPermissionService;
import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.monitor.domain.BusWaterAutomonitorevaluateHourdata;
import com.ruoyi.monitor.service.IBusWaterAutomonitorevaluateHourdataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.utils.DateUtils.differentDaysByMillisecond;
import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 数据共享Controller
 *
 * @author ruoyi
 * @date 2025-08-08
 */
@RestController
@RequestMapping("/system/DataShare")
public class DataShareController extends BaseController {
    @Autowired
    private IDataShareService dataShareService;
    @Autowired
    private IBusWaterSegmentService busWaterSegmentService;
    @Autowired
    private ISysPermissionService sysPermissionService;
    @Autowired
    private IBusWaterAutomonitorevaluateHourdataService busWaterAutomonitorevaluateHourdataService;

    /**
     * 获取断面数据
     *
     * @return
     */
    @RateLimiter(key = "/system/DataShare/BusWaterSegmentlist", time = 5, count = 1)
    @GetMapping("/BusWaterSegmentlist")
    public AjaxResult BusWaterSegmentlist(String appsecret) {
        boolean check =  dataShareService.getCheckAppsecret(appsecret);
        if(!check){
            return AjaxResult.error("未授权");
        }
        List<BusWaterSegment> list = busWaterSegmentService.selectBusWaterSegmentList(new BusWaterSegment());
        SysPermission sysPermission = new SysPermission();
        sysPermission.setPath("/system/DataShare/BusWaterSegmentlist");
        List<SysPermission> SysPermissionlist = sysPermissionService.selectSysPermissionList(sysPermission);
        if (!SysPermissionlist.isEmpty()) {
            sysPermission = SysPermissionlist.get(0);
            sysPermission.setCallcount(sysPermission.getCallcount() + 1);
            Integer score =  sysPermissionService.getSysPermissionScore(sysPermission);
            sysPermission.setScore(score);
            sysPermissionService.updateSysPermission(sysPermission);
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取自动监测数据
     *
     * @param
     * @return
     */
    @RateLimiter(key = "/system/DataShare/autovaluatehourdatalist", time = 5, count = 1)
    @GetMapping("/autovaluatehourdatalist")
    public AjaxResult autovaluatehourdatalist(String startDate, String endDate,String appsecret) {
        boolean check =  dataShareService.getCheckAppsecret(appsecret);
        if(!check){
            return AjaxResult.error("未授权");
        }
        Date beginDate = DateUtils.parseDate(startDate);
        Date eDate = DateUtils.parseDate(endDate);
        if (differentDaysByMillisecond(beginDate, eDate) > 7) {
            return AjaxResult.error("开始时间与结束时间不能超过7天");
        }
        BusWaterAutomonitorevaluateHourdata busWaterAutomonitorevaluateHourdata = new BusWaterAutomonitorevaluateHourdata();
        busWaterAutomonitorevaluateHourdata.setStartTime(beginDate);
        busWaterAutomonitorevaluateHourdata.setEndTime(eDate);
        List<Map> list = busWaterAutomonitorevaluateHourdataService.selectBusWaterAutomonitorevaluateHourdataAndItemList(busWaterAutomonitorevaluateHourdata);
        SysPermission sysPermission = new SysPermission();
        sysPermission.setPath("/system/DataShare/autovaluatehourdatalist");
        List<SysPermission> SysPermissionlist = sysPermissionService.selectSysPermissionList(sysPermission);
        if (!SysPermissionlist.isEmpty()) {
            sysPermission = SysPermissionlist.get(0);
            sysPermission.setCallcount(sysPermission.getCallcount() + 1);
            Integer score =  sysPermissionService.getSysPermissionScore(sysPermission);
            sysPermission.setScore(score);
            sysPermissionService.updateSysPermission(sysPermission);
        }
        return AjaxResult.success(list);
    }
}
